# encoding=utf-8
from LBT.BaseParent.BaseLbt import lbtlogin
from LBT.Resource.StudentAttendance.SchoolAttendanceAtt import SchoolAttendanceAtt


class SchoolAttendance(lbtlogin, SchoolAttendanceAtt):
    '''全园考勤统计模块
    1、数据库查询有考勤数据的最后日期，然后去前台页面校验。
    2、默认查询，然后导出，并校验数据库
    '''

    @lbtlogin.loginFun
    def test_checkLastAtt(self):
        """
        数据库查询有考勤数据的最后日期，然后去前台页面校验。
        :return:
        """
        self.docstr = '数据库查询有考勤数据的最后日期，然后去前台页面校验。'
        dr = self.driver
        self.moduleOp.openMoudleAndIn(self.studentatten_navigation_class, self.studentatten_school_url,
                                      self.student_topnavigation_class)
        dr.find_element_by_css_selector(self.schoolatt_selectbtn_css).click()
        resultinfo={}
        pages=dr.find_elements_by_xpath(self.schoolatt_page_xpath).__len__()-4
        #找到第一個有考勤数据的日期，查询出勤人数并校验。如果第一页没有考勤数据，就翻页
        for i in range(pages):
            isbreak=False
            results = dr.find_elements_by_xpath(self.schoolatt_results_xpath)
            for result in results:
                print(result.text)
                resultinfo["innum"] = int(result.text.split(" ")[2])
                if resultinfo["innum"]>0:
                    resultinfo["datestr"]=str(result.text).split(" ")[0]
                    resultinfo["notinnum"] = int(result.text.split(" ")[3])
                    isbreak=True
                    break
            if isbreak:
                sql = "select count(1)from ( select distinct(studentid) from attendance where schoolid in (select schoolid from school where schoolname = '" + self.lbt_schoolname_text + "') and convert(varchar(10),cardtime,121)='" + \
                      resultinfo["datestr"] + "')a"
                self.assertTrue(int(self.selectlbt(sql)) == resultinfo["innum"])
                sql = "select count(1) from users where  schoolid in (select schoolid from school where schoolname = '" + self.lbt_schoolname_text + "') and state=1 and usertype =2"
                self.assertTrue((int(self.selectlbt(sql)) - resultinfo["innum"]) == resultinfo["notinnum"])
                break
            elif i<(pages-1):
                ele = dr.find_element_by_xpath(self.schoolatt_pagenext_xpath)
                ele.location_once_scrolled_into_view
                ele.click()

    @lbtlogin.loginFun
    def test_checkExport(self):
        """
        默认查询，然后导出，并校验数据库
        :return:
        """
        self.docstr = '默认查询，然后导出，并校验数据库'
        dr = self.driver
        self.moduleOp.openMoudleAndIn(self.studentatten_navigation_class, self.studentatten_school_url,
                                      self.student_topnavigation_class)
        dr.find_element_by_css_selector(self.schoolatt_selectbtn_css).click()
        listinfo=[]
        pages = dr.find_elements_by_xpath(self.schoolatt_page_xpath).__len__() - 4
        #根据页数返回获取所有数据
        for i in range(pages):
            results = dr.find_elements_by_xpath(self.schoolatt_results_xpath)
            for result in results:
                resultinfo = {}
                resultinfo["innum"] = int(result.text.split(" ")[2])
                resultinfo["datestr"] = str(result.text).split(" ")[0]
                resultinfo["notinnum"] = int(result.text.split(" ")[3])
                listinfo.append(resultinfo)
            if i<(pages-1):
                ele = dr.find_element_by_xpath(self.schoolatt_pagenext_xpath)
                ele.location_once_scrolled_into_view
                ele.click()
        sql = "select count(1) from users where  schoolid in (select schoolid from school where schoolname = '" + self.lbt_schoolname_text + "') and state=1 and usertype =2"
        totalnum=int(self.selectlbt(sql))
        #比对页面数据和数据库
        for info in listinfo:
            sql = "select count(1)from ( select distinct(studentid) from attendance where schoolid in (select schoolid from school where schoolname = '" + self.lbt_schoolname_text + "') and convert(varchar(10),cardtime,121)='" + \
                  info["datestr"] + "')a"
            self.assertTrue(int(self.selectlbt(sql)) == info["innum"])
            self.assertTrue((totalnum - info["innum"]) == info["notinnum"])
        #比对页面数据和导出数据
        self.deleteAllFiles(self.defaultdownpath)
        dr.find_element_by_css_selector(self.schoolatt_exportbtn_css).click()
        list1 = self.readExcle(self.defaultdownpath)
        listnew=list(list1[1:])
        listfinally=[]
        for i in range(listnew.__len__()):
            finallyinfo={}
            finallyinfo["innum"] = int(listnew[i][1])
            finallyinfo["datestr"] = str(listnew[i][0]).split(" ")[0]
            finallyinfo["notinnum"] = int(listnew[i][2])
            listfinally.append(finallyinfo)
        self.assertTrue(listfinally==listinfo)